/*
 * ContasAReceber.java
 *
 * Created on 04/08/2010, 15:31:27
 * Copyright LAS
 */
package Soligee.view;

import Soligee.controller.facade.sessionFacadeImplements.SessionFacadeAlterarImplements;
import Soligee.controller.facade.sessionFacadeImplements.SessionFacadeBuscarImplements;
import Soligee.controller.facade.sessionFacadeImplements.SessionFacadeCadastrarImplements;
import Soligee.decorador.PaginacaoListaDezEmDez;
import Soligee.model.vo.cadastros.ParcelaVendaVO;
import Soligee.model.vo.lancamentos.VendaVO;
import com.icesoft.faces.component.ext.HtmlCommandButton;
import com.icesoft.faces.component.ext.HtmlDataTable;
import com.icesoft.faces.component.ext.HtmlInputText;
import com.icesoft.faces.component.ext.HtmlOutputText;
import com.icesoft.faces.component.jsfcl.data.DefaultTableDataModel;
import com.icesoft.faces.component.jsfcl.data.SelectInputDateBean;
import com.icesoft.faces.component.panelpopup.PanelPopup;
import com.icesoft.faces.component.selectinputdate.SelectInputDate;
import com.sun.rave.web.ui.appbase.AbstractPageBean;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.FacesException;


/**
 * <p>Page bean that corresponds to a similarly named JSP page.  This
 * class contains component definitions (and initialization code) for
 * all components that you have defined on this page, as well as
 * lifecycle methods and event handlers where you may add behavior
 * to respond to incoming events.</p>
 */
public class ContasAReceber extends AbstractPageBean {
    // <editor-fold defaultstate="collapsed" desc="Managed Component Definition">
    private int __placeholder;
    private List<ParcelaVendaVO> listaDeParcelas;
    private SessionFacadeBuscarImplements facadeBusca = new SessionFacadeBuscarImplements();
    private static ParcelaVendaVO parcelaVenda;
    private static VendaVO venda;
    private static PaginacaoListaDezEmDez paginacaoBusca;
    private static int controle;

    public static PaginacaoListaDezEmDez getPaginacaoBusca() {
        return paginacaoBusca;
    }

    public static void setPaginacaoBusca(PaginacaoListaDezEmDez paginacaoBusca) {
        ContasAReceber.paginacaoBusca = paginacaoBusca;
    }
    /**
     * <p>Automatically managed component initialization.  <strong>WARNING:</strong>
     * This method is automatically generated, so any user-specified code inserted
     * here is subject to being replaced.</p>
     */
    private void _init() throws Exception {

    }
    public List<ParcelaVendaVO> getListaDeParcelas() {
        return listaDeParcelas;
    }

    public void setListaDeParcelas(List<ParcelaVendaVO> listaDeParcelas) {
        this.listaDeParcelas = listaDeParcelas;
    }
    private HtmlInputText campoCodVenda = new HtmlInputText();

    public HtmlInputText getCampoCodVenda() {
        return campoCodVenda;
    }

    public void setCampoCodVenda(HtmlInputText hit) {
        this.campoCodVenda = hit;
    }
    private HtmlInputText campoNomeCliente = new HtmlInputText();

    public HtmlInputText getCampoNomeCliente() {
        return campoNomeCliente;
    }

    public void setCampoNomeCliente(HtmlInputText hit) {
        this.campoNomeCliente = hit;
    }
    private HtmlInputText campoTipoCliente = new HtmlInputText();

    public HtmlInputText getCampoTipoCliente() {
        return campoTipoCliente;
    }

    public void setCampoTipoCliente(HtmlInputText hit) {
        this.campoTipoCliente = hit;
    }
    private HtmlInputText campoFormaPagamento = new HtmlInputText();

    public HtmlInputText getCampoFormaPagamento() {
        return campoFormaPagamento;
    }

    public void setCampoFormaPagamento(HtmlInputText hit) {
        this.campoFormaPagamento = hit;
    }
    private HtmlInputText campoNomeVendedor = new HtmlInputText();

    public HtmlInputText getCampoNomeVendedor() {
        return campoNomeVendedor;
    }

    public void setCampoNomeVendedor(HtmlInputText hit) {
        this.campoNomeVendedor = hit;
    }
    private HtmlInputText campoQtdParcelas = new HtmlInputText();

    public HtmlInputText getCampoQtdParcelas() {
        return campoQtdParcelas;
    }

    public void setCampoQtdParcelas(HtmlInputText hit) {
        this.campoQtdParcelas = hit;
    }
    private HtmlInputText campoValorTotal = new HtmlInputText();

    public HtmlInputText getCampoValorTotal() {
        return campoValorTotal;
    }

    public void setCampoValorTotal(HtmlInputText hit) {
        this.campoValorTotal = hit;
    }
    private SelectInputDateBean selectInputDate1Bean = new SelectInputDateBean();

    public SelectInputDateBean getSelectInputDate1Bean() {
        return selectInputDate1Bean;
    }

    public void setSelectInputDate1Bean(SelectInputDateBean sidb) {
        this.selectInputDate1Bean = sidb;
    }
    private DefaultTableDataModel dataTable1Model = new DefaultTableDataModel();

    public DefaultTableDataModel getDataTable1Model() {
        return dataTable1Model;
    }

    public void setDataTable1Model(DefaultTableDataModel dtdm) {
        this.dataTable1Model = dtdm;
    }
    private SelectInputDateBean selectInputDateBean1 = new SelectInputDateBean();

    public SelectInputDateBean getSelectInputDateBean1() {
        return selectInputDateBean1;
    }

    public void setSelectInputDateBean1(SelectInputDateBean sidb) {
        this.selectInputDateBean1 = sidb;
    }
    private PanelPopup painelPopUpAviso = new PanelPopup();

    public PanelPopup getPainelPopUpAviso() {
        return painelPopUpAviso;
    }

    public void setPainelPopUpAviso(PanelPopup pp) {
        this.painelPopUpAviso = pp;
    }
    private HtmlDataTable tabelaVendas = new HtmlDataTable();

    public HtmlDataTable getTabelaVendas() {
        return tabelaVendas;
    }

    public void setTabelaVendas(HtmlDataTable hdt) {
        this.tabelaVendas = hdt;
    }
    private SelectInputDate dataEntrada = new SelectInputDate();

    public SelectInputDate getDataEntrada() {
        return dataEntrada;
    }

    public void setDataEntrada(SelectInputDate sid) {
        this.dataEntrada = sid;
    }
    private HtmlInputText campoNumParcela = new HtmlInputText();

    public HtmlInputText getCampoNumParcela() {
        return campoNumParcela;
    }

    public void setCampoNumParcela(HtmlInputText hit) {
        this.campoNumParcela = hit;
    }
    private HtmlInputText campoSituacao = new HtmlInputText();

    public HtmlInputText getCampoSituacao() {
        return campoSituacao;
    }

    public void setCampoSituacao(HtmlInputText hit) {
        this.campoSituacao = hit;
    }
    private HtmlCommandButton btnPopUp1 = new HtmlCommandButton();

    public HtmlCommandButton getBtnPopUp1() {
        return btnPopUp1;
    }

    public void setBtnPopUp1(HtmlCommandButton hcb) {
        this.btnPopUp1 = hcb;
    }
    private HtmlCommandButton btnPopUp2 = new HtmlCommandButton();

    public HtmlCommandButton getBtnPopUp2() {
        return btnPopUp2;
    }

    public void setBtnPopUp2(HtmlCommandButton hcb) {
        this.btnPopUp2 = hcb;
    }
    private HtmlOutputText textoPopUp = new HtmlOutputText();

    public HtmlOutputText getTextoPopUp() {
        return textoPopUp;
    }

    public void setTextoPopUp(HtmlOutputText hot) {
        this.textoPopUp = hot;
    }
    private HtmlCommandButton botaoReceberConta = new HtmlCommandButton();

    public HtmlCommandButton getBotaoReceberConta() {
        return botaoReceberConta;
    }

    public void setBotaoReceberConta(HtmlCommandButton hcb) {
        this.botaoReceberConta = hcb;
    }
    private HtmlCommandButton tabelaVendasFooterFirstButton = new HtmlCommandButton();

    public HtmlCommandButton getTabelaVendasFooterFirstButton() {
        return tabelaVendasFooterFirstButton;
    }

    public void setTabelaVendasFooterFirstButton(HtmlCommandButton hcb) {
        this.tabelaVendasFooterFirstButton = hcb;
    }
    private HtmlCommandButton tabelaVendasFooterPreviousButton = new HtmlCommandButton();

    public HtmlCommandButton getTabelaVendasFooterPreviousButton() {
        return tabelaVendasFooterPreviousButton;
    }

    public void setTabelaVendasFooterPreviousButton(HtmlCommandButton hcb) {
        this.tabelaVendasFooterPreviousButton = hcb;
    }
    private HtmlCommandButton tabelaVendasFooterNextButton = new HtmlCommandButton();

    public HtmlCommandButton getTabelaVendasFooterNextButton() {
        return tabelaVendasFooterNextButton;
    }

    public void setTabelaVendasFooterNextButton(HtmlCommandButton hcb) {
        this.tabelaVendasFooterNextButton = hcb;
    }
    private HtmlCommandButton tabelaVendasFooterLastButton = new HtmlCommandButton();

    public HtmlCommandButton getTabelaVendasFooterLastButton() {
        return tabelaVendasFooterLastButton;
    }

    public void setTabelaVendasFooterLastButton(HtmlCommandButton hcb) {
        this.tabelaVendasFooterLastButton = hcb;
    }

    // </editor-fold>

    /**
     * <p>Construct a new Page bean instance.</p>
     */
    public ContasAReceber() {
    }

    /**
     * <p>Callback method that is called whenever a page is navigated to,
     * either directly via a URL, or indirectly via page navigation.
     * Customize this method to acquire resources that will be needed
     * for event handlers and lifecycle methods, whether or not this
     * page is performing post back processing.</p>
     * 
     * <p>Note that, if the current request is a postback, the property
     * values of the components do <strong>not</strong> represent any
     * values submitted with this request.  Instead, they represent the
     * property values that were saved for this view when it was rendered.</p>
     */
    @Override
    public void init() {
        // Perform initializations inherited from our superclass
        super.init();
        inicializaCampos();

        
        // Perform application initialization that must complete
        // *before* managed components are initialized
        // TODO - add your own initialiation code here
            
        // <editor-fold defaultstate="collapsed" desc="Managed Component Initialization">
        // Initialize automatically managed components
        // *Note* - this logic should NOT be modified
        try {
            _init();
        } catch (Exception e) {
            log("ContasAReceber Initialization Failure", e);
            throw e instanceof FacesException ? (FacesException) e: new FacesException(e);
        }
        
        // </editor-fold>
        // Perform application initialization that must complete
        // *after* managed components are initialized
        // TODO - add your own initialization code here
    }

    /**
     * <p>Callback method that is called after the component tree has been
     * restored, but before any event processing takes place.  This method
     * will <strong>only</strong> be called on a postback request that
     * is processing a form submit.  Customize this method to allocate
     * resources that will be required in your event handlers.</p>
     */
    @Override
    public void preprocess() {
    }

    /**
     * <p>Callback method that is called just before rendering takes place.
     * This method will <strong>only</strong> be called for the page that
     * will actually be rendered (and not, for example, on a page that
     * handled a postback and then navigated to a different page).  Customize
     * this method to allocate resources that will be required for rendering
     * this page.</p>
     */
    @Override
    public void prerender() {
    }

    /**
     * <p>Callback method that is called after rendering is completed for
     * this request, if <code>init()</code> was called (regardless of whether
     * or not this was the page that was actually rendered).  Customize this
     * method to release resources acquired in the <code>init()</code>,
     * <code>preprocess()</code>, or <code>prerender()</code> methods (or
     * acquired during execution of an event handler).</p>
     */
    @Override
    public void destroy() {
    }

    /**
     * <p>Retornar uma referência para o Bean de dados limitados.</p>
     *
     * @retorna uma referência para o bean de dados limitados
     */
    protected SessionBean1 getview$SessionBean1() {
        return (SessionBean1) getBean("view$SessionBean1");
    }

    /**
     * <p>Retornar uma referência para o Bean de dados limitados.</p>
     *
     * @retorna uma referência para o bean de dados limitados
     */
    protected RequestBean1 getview$RequestBean1() {
        return (RequestBean1) getBean("view$RequestBean1");
    }

    /**
     * <p>Retornar uma referência para o Bean de dados limitados.</p>
     *
     * @retorna uma referência para o bean de dados limitados
     */
    protected ApplicationBean1 getview$ApplicationBean1() {
        return (ApplicationBean1) getBean("view$ApplicationBean1");
    }

    public String linkAction1_action() {
        botaoReceberConta.setRendered(true);
        parcelaVenda = (ParcelaVendaVO) tabelaVendas.getRowData();
        venda =parcelaVenda.getVenda();
        campoCodVenda.setValue(venda.getCodVenda());
        dataEntrada.setValue(venda.getDataVenda());
        campoNomeCliente.setValue(venda.getPessoa().getNome());
        campoNomeVendedor.setValue(venda.getVendedor().getNome());
        campoFormaPagamento.setValue(venda.getFormaPagamento().getFormaPagamento());
        campoQtdParcelas.setValue(parcelaVenda.getNumeroDeParcelas());
        campoNumParcela.setValue(parcelaVenda.getNumParcela());
        campoValorTotal.setValue(parcelaVenda.getValorParcela());
        if(parcelaVenda.getSituacao()==0)
            campoSituacao.setValue("Pendente");
        else if(parcelaVenda.getSituacao()==1)
            campoSituacao.setValue("Pago");
        else
            campoSituacao.setValue("Vencido");
        
        return null;
    }

    public void setarCampos(boolean x){
        campoCodVenda.setReadonly(x);
        dataEntrada.setDisabled(x);
        campoNomeVendedor.setReadonly(x);
        campoNomeCliente.setReadonly(x);
        campoFormaPagamento.setReadonly(x);
        campoQtdParcelas.setReadonly(x);
        campoNumParcela.setReadonly(x);
        campoValorTotal.setReadonly(x);
        campoSituacao.setReadonly(x);
    }

    public void limparCamposVenda(){
        campoCodVenda.resetValue();
        dataEntrada.resetValue();
        campoNomeVendedor.resetValue();
        campoNomeCliente.resetValue();
        campoFormaPagamento.resetValue();
        campoQtdParcelas.resetValue();
        campoNumParcela.resetValue();
        campoValorTotal.resetValue();
        campoSituacao.resetValue();
    }

    public void inicializaCampos(){
        controle=0;
        limparCamposVenda();
        setarCampos(true);
        painelPopUpAviso.setRendered(false);
        atualizaTabelaVenda();
        botaoReceberConta.setRendered(false);
    }

    private void atualizaTabelaVenda(){
            this.setListaDeParcelas(facadeBusca.buscarParcelaPendente("dataVencimento",ParcelaVendaVO.class));
            dataTable1Model.setWrappedData(getListaDeParcelas());
            this.setPaginacaoBusca(new PaginacaoListaDezEmDez(this.getListaDeParcelas()));
            this.controlaPaginacaoNf(this.getListaDeParcelas().size());
    }

    public void controlaPopup(boolean renderedPopup,String textoPoup,boolean btn1Rendered, boolean btn2Rendered,
            String textobtn1, String textobtn2){
        painelPopUpAviso.setRendered(renderedPopup);
        textoPopUp.setValue(textoPoup);
        btnPopUp1.setValue(textobtn1);
        btnPopUp1.setRendered(btn1Rendered);
        btnPopUp2.setRendered(btn2Rendered);
        btnPopUp2.setValue(textobtn2);
    }
    public String botaoReceberConta_action() {
        controle=0;
        controlaPopup(true, "Receber Pgto. de Parcela?", true, true, "Sim", "Não");
        return null;
    }

    public String btnPopUp2_action() {
        controlaPopup(false, "", false ,false, "", "");
        return null;
    }

    public String btnPopUp1_action() {
        if(controle==1){
            return "voltar";
        }
        else{
      parcelaVenda.setSituacao(1);
      SessionFacadeAlterarImplements facadeAlterar = new SessionFacadeAlterarImplements();
        try {
            facadeAlterar.alterarParcelaVenda(parcelaVenda);
        } catch (Exception ex) {
            Logger.getLogger(ContasAReceber.class.getName()).log(Level.SEVERE, null, ex);
        }
      controlaPopup(true, "Parcela Paga com Sucesso!", false, true, "", "Ok");
      limparCamposVenda();
      atualizaTabelaVenda();
      botaoReceberConta.setRendered(false);
      return null;
        }
    }
    

    public String tabelaVendas_firstPageAction() {
        {
            dataTable1Model.setWrappedData(ContasAReceber.getPaginacaoBusca().firstPage());
            tabelaVendasFooterFirstButton.setDisabled(true);
            tabelaVendasFooterPreviousButton.setDisabled(true);
            if (ContasAReceber.getPaginacaoBusca().getTotalPaginas() > 1) {
                tabelaVendasFooterNextButton.setDisabled(false);
                tabelaVendasFooterLastButton.setDisabled(false);
            }
            return null;
        }
    }

    public String tabelaVendas_previousPageAction() {
        dataTable1Model.setWrappedData(ContasAReceber.getPaginacaoBusca().previousPage());
        if (ContasAReceber.getPaginacaoBusca().getNumeroDaPagina() == 1) {
            tabelaVendasFooterNextButton.setDisabled(true);
            tabelaVendasFooterFirstButton.setDisabled(true);
            tabelaVendasFooterPreviousButton.setDisabled(true);
        }
        if (ContasAReceber.getPaginacaoBusca().getTotalPaginas() > 1) {
            tabelaVendasFooterNextButton.setDisabled(false);
            tabelaVendasFooterLastButton.setDisabled(false);
        }
        if (ContasAReceber.getPaginacaoBusca().getNumeroDaPagina() >= 2) {
            tabelaVendasFooterFirstButton.setDisabled(false);
        }
        return null;
    }

    public String tabelaVendas_nextPageAction() {
        dataTable1Model.setWrappedData(ContasAReceber.getPaginacaoBusca().nextPage());
        tabelaVendasFooterPreviousButton.setDisabled(false);
        if (ContasAReceber.getPaginacaoBusca().getTotalPaginas() == ContasAReceber.getPaginacaoBusca().getNumeroDaPagina()) {
            tabelaVendasFooterNextButton.setDisabled(true);
            tabelaVendasFooterLastButton.setDisabled(true);
        }
        if (ContasAReceber.getPaginacaoBusca().getNumeroDaPagina() >= 2) {
            tabelaVendasFooterFirstButton.setDisabled(false);
        }
         return null;
    }

    public String tabelaVendas_lastPageAction() {
        dataTable1Model.setWrappedData(ContasAReceber.getPaginacaoBusca().lastPage());
        if (ContasAReceber.getPaginacaoBusca().getTotalPaginas() == ContasAReceber.getPaginacaoBusca().getNumeroDaPagina()) {
            tabelaVendasFooterLastButton.setDisabled(true);
            tabelaVendasFooterNextButton.setDisabled(true);
            tabelaVendasFooterPreviousButton.setDisabled(false);
            tabelaVendasFooterFirstButton.setDisabled(false);
        }
        if (ContasAReceber.getPaginacaoBusca().getNumeroDaPagina() >= 2) {
            tabelaVendasFooterFirstButton.setDisabled(false);
        }
        return null;
    }

    public void controlaPaginacaoNf(int controlePaginas) {
      if (controlePaginas <= 10) {
         tabelaVendasFooterFirstButton.setDisabled(true);
         tabelaVendasFooterPreviousButton.setDisabled(true);
         tabelaVendasFooterNextButton.setDisabled(true);
         tabelaVendasFooterLastButton.setDisabled(true);
      }
      if (controlePaginas > 10) {
         tabelaVendasFooterNextButton.setDisabled(false);
         tabelaVendasFooterLastButton.setDisabled(false);
         tabelaVendasFooterFirstButton.setDisabled(true);
         tabelaVendasFooterPreviousButton.setDisabled(true);
      }
    }

    public String btn_voltar_action(){
        controle=1;
        controlaPopup(true, "Deseja Sair ?", true, true, "Sim", "Não");
        return null;
    }
    public String panelLayoutLogo_action(){
        return "voltar";
    }
    public void linkCancelar_action(){
        botaoReceberConta.setRendered(false);
        limparCamposVenda();
    }

}

